package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

public class BaseUtil {
    public static int goPage(Application application, javafx.stage.Stage stage) {
        try {
            application.start(stage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    public static <T extends Application> int closeGoPage(javafx.stage.Stage current, Class<T> application, javafx.stage.Stage stage) {
        try {
            T instance = application.newInstance();
            current.close();
            instance.start(stage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static int hideGoPage(javafx.stage.Stage current, Application application, javafx.stage.Stage stage) {
        try {
//            current.close();
            application.start(stage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static void info(String msg) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION, msg);
        alert.show();
    }

    public static void err(String msg) {
        Alert alert = new Alert(Alert.AlertType.ERROR, msg);
        alert.show();
    }

    public static void warning(String msg) {
        Alert alert = new Alert(Alert.AlertType.WARNING, msg);
        alert.show();
    }

    public static boolean dialog(String msg) {
        ButtonType sure = new ButtonType("确定");
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, msg, sure, new ButtonType("取消"));
        Optional<ButtonType> buttonType = alert.showAndWait();
        if (buttonType.get() == sure) {
            return true;
        }
        return false;
    }

    public static void gotoMain(boolean flag, String msg, Stage stage) {
        if (flag && stage != null) {
            stage.close();
        } else {
            info(msg);
        }
    }

    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    public static String formatDate(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String between(Date start, Date end) {
        long bw = end.getTime() - start.getTime();
        long tmp = bw / 1000;
        long minus = tmp / 60;
        long seconds = tmp % 60;
        return String.format("%s分钟%s秒", minus, seconds);
    }

    public static void clearText(TextField... fields) {
        for (TextField field : fields) {
            field.setText("");
        }
    }

    public static void commonHandle(javafx.stage.Stage primaryStage, String xml, String title) throws java.io.IOException {
        if (primaryStage == null) {
            primaryStage = new javafx.stage.Stage();
        }
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
        fxmlLoader.setLocation(BaseUtil.class.getResource(xml));
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setTitle(title);
        BaseController controller = fxmlLoader.getController();
        controller.init();
        controller.setStage(primaryStage);
        primaryStage.show();
    }
}
